package com.bicon.life.core.base.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ReflectGetValue {
    /**
     * 取出bean 属性和值
     * @param obj
     * @return
     * @throws Exception
     */
    public static Map<Object,Object> getFileValue(Object obj) throws Exception{
        Map<Object, Object> map = new HashMap<Object, Object>();
        Class<?> cls = obj.getClass();
        Method methods[] = cls.getDeclaredMethods();
        Field fields[] = cls.getDeclaredFields();

        for(Field field:fields){

            String fldtype = field.getType().getSimpleName();
            String getMetName = pareGetName(field.getName());
            String result ="";
            if(!checkMethod(methods,getMetName)){
                continue;
            }
            Method method = cls.getMethod(getMetName, null);
            Object object = method.invoke(obj, new Object[]{});
            if(null != object){
                if(fldtype.equals("Date")){
                    result = fmlDate((Date)object);
                }else{
                    result = String.valueOf(object);
                }

            }
            map.put(field.getName(), result);
        }
        return map;
    }
    /**
     * 设置bean 属性值
     * @param bean
     * @throws Exception
     */
    public static void setFieldValue( Object bean,Object... objs) throws Exception{

        if(objs.length>0){
            Map<Object, Object> maps=new HashMap<>();
            for(int i=0;i<objs.length;i++){
                maps.putAll(getFileValue(objs[i]));
            }
            if(maps.size()>0){
                Class<?> cls = bean.getClass();
                Method methods[] = cls.getDeclaredMethods();
                Field fields[] = cls.getDeclaredFields();

                for(Field field:fields){
                    String fldtype = field.getType().getSimpleName();
                    String fldSetName = field.getName();
                    String setMethod = pareSetName(fldSetName);
                    if(!checkMethod(methods, setMethod)){
                        continue;
                    }
                    Object value = maps.get(fldSetName);
                    Method fieldSetMet = cls.getMethod(setMethod, field.getType());
                    if (null != value && !"".equals(value)) {
                        String fieldType = field.getType().getSimpleName();
                        if ("String".equals(fieldType)) {
                            fieldSetMet.invoke(bean, value);
                        } else if ("Date".equals(fieldType)) {
                            Date temp = parseDate((String)value);
                            fieldSetMet.invoke(bean, temp);
                        } else if ("Integer".equals(fieldType) || "int".equals(fieldType)) {
                            Integer intval = Integer.parseInt((String)value);
                            fieldSetMet.invoke(bean, intval);
                        } else if ("Long".equalsIgnoreCase(fieldType)) {
                            Long temp = Long.parseLong((String)value);
                            fieldSetMet.invoke(bean, temp);
                        } else if ("Double".equalsIgnoreCase(fieldType)) {
                            Double temp = Double.parseDouble((String)value);
                            fieldSetMet.invoke(bean, temp);
                        } else if ("Boolean".equalsIgnoreCase(fieldType)) {
                            Boolean temp = Boolean.parseBoolean((String)value);
                            fieldSetMet.invoke(bean, temp);
                        } else {
                            System.out.println("not supper type" + fieldType);
                        }
                    }

                }
            }
        }

    }

    /**
     * 格式化string为Date
     * @param datestr
     * @return date
     */
    private static Date parseDate(String datestr) {
        if (null == datestr || "".equals(datestr)) {
            return null;
        }
        try {
            String fmtstr = null;
            if (datestr.indexOf(':') > 0) {
                fmtstr = "yyyy-MM-dd HH:mm:ss";
            } else {
                fmtstr = "yyyy-MM-dd";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(fmtstr);
            return sdf.parse(datestr);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 拼接某属性get 方法
     * @param fldname
     * @return
     */
    public static String pareGetName(String fldname){
        if(null == fldname || "".equals(fldname)){
            return null;
        }
        String pro = "get"+fldname.substring(0,1).toUpperCase()+fldname.substring(1);
        return pro;
    }
    /**
     * 拼接某属性set 方法
     * @param fldname
     * @return
     */
    public static String pareSetName(String fldname){
        if(null == fldname || "".equals(fldname)){
            return null;
        }
        String pro = "set"+fldname.substring(0,1).toUpperCase()+fldname.substring(1);
        return pro;
    }
    /**
     * 判断该方法是否存在
     * @param methods
     * @param met
     * @return
     */
    public static boolean checkMethod(Method methods[],String met){
        if(null != methods ){
            for(Method method:methods){
                if(met.equals(method.getName())){
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 把date 类转换成string
     * @param date
     * @return
     */
    public static String fmlDate(Date date){
        if(null != date){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        }
        return null;
    }
}